﻿using System;
using System.Data;
using System.Collections.Generic;
using Chenxin.Lib.Business.Entity;
using Chenxin.Lib.Utility;
using Chenxin.Lib.Business;
using System.Text;

namespace Chenxin.Lib.Business.BLL
{
    /// <summary>
    /// 业务逻辑类t_Preliminary_BaseInfo 的摘要说明。
    /// </summary>
    public class PreliminaryBaseInfo
    {
        public PreliminaryBaseInfo()
        { }
        #region  成员方法
        /// <summary>
        /// 增加一条数据
        /// </summary>
        public void Add(Chenxin.Lib.Business.Entity.t_Preliminary_BaseInfo model)
        {
            Chenxin.Lib.Business.DAL.t_Preliminary_BaseInfo.Add(model);
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public void Update(Chenxin.Lib.Business.Entity.t_Preliminary_BaseInfo model)
        {
            Chenxin.Lib.Business.DAL.t_Preliminary_BaseInfo.Update(model);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public void Delete(Guid RecordId)
        {

            Chenxin.Lib.Business.DAL.t_Preliminary_BaseInfo.Delete(RecordId);
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Chenxin.Lib.Business.Entity.t_Preliminary_BaseInfo GetModel(Guid RecordId)
        {

            return Chenxin.Lib.Business.DAL.t_Preliminary_BaseInfo.GetModel(RecordId);
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Chenxin.Lib.Business.Entity.t_Preliminary_BaseInfo GetOldModel(Guid HouseId)
        {

            return Chenxin.Lib.Business.DAL.t_Preliminary_BaseInfo.GetOldModel(HouseId);
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Chenxin.Lib.Business.Entity.t_Preliminary_BaseInfo GetLatestModelByHouseId(string HouseId)
        {

            return Chenxin.Lib.Business.DAL.t_Preliminary_BaseInfo.GetLatestModelByHouseId(HouseId);
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Chenxin.Lib.Business.Entity.t_Preliminary_BaseInfo GetOldModelByAddress(String Address)
        {

            return Chenxin.Lib.Business.DAL.t_Preliminary_BaseInfo.GetOldModelByAddress(Address);
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            return Chenxin.Lib.Business.DAL.t_Preliminary_BaseInfo.GetList(strWhere);
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<Chenxin.Lib.Business.Entity.t_Preliminary_BaseInfo> GetModelList(string strWhere)
        {
            DataSet ds = Chenxin.Lib.Business.DAL.t_Preliminary_BaseInfo.GetList(strWhere);
            return DataTableToList(ds.Tables[0]);
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<Chenxin.Lib.Business.Entity.t_Preliminary_BaseInfo> DataTableToList(DataTable dt)
        {
            List<Chenxin.Lib.Business.Entity.t_Preliminary_BaseInfo> modelList = new List<Chenxin.Lib.Business.Entity.t_Preliminary_BaseInfo>();
            int rowsCount = dt.Rows.Count;
            if (rowsCount > 0)
            {
                Chenxin.Lib.Business.Entity.t_Preliminary_BaseInfo model;
                for (int n = 0; n < rowsCount; n++)
                {
                    model = new Chenxin.Lib.Business.Entity.t_Preliminary_BaseInfo();
					if(dt.Rows[n]["RecordId"].ToString()!="")
					{
						model.RecordId=new Guid(dt.Rows[n]["RecordId"].ToString());
					}
					if(dt.Rows[n]["HouseId"].ToString()!="")
					{
						model.HouseId=new Guid(dt.Rows[n]["HouseId"].ToString());
					}
					model.HouseCode=dt.Rows[n]["HouseCode"].ToString();
					model.Address=dt.Rows[n]["Address"].ToString();
					if(dt.Rows[n]["BuildingArea"].ToString()!="")
					{
						model.BuildingArea=decimal.Parse(dt.Rows[n]["BuildingArea"].ToString());
					}
					model.BuildingType=dt.Rows[n]["BuildingType"].ToString();
					if(dt.Rows[n]["SalePrice_T"].ToString()!="")
					{
						model.SalePrice_T=decimal.Parse(dt.Rows[n]["SalePrice_T"].ToString());
					}
					if(dt.Rows[n]["SalePrice_U"].ToString()!="")
					{
						model.SalePrice_U=decimal.Parse(dt.Rows[n]["SalePrice_U"].ToString());
					}
					if(dt.Rows[n]["CompleteYear"].ToString()!="")
					{
						model.CompleteYear=int.Parse(dt.Rows[n]["CompleteYear"].ToString());
					}
					if(dt.Rows[n]["Total_Floor"].ToString()!="")
					{
						model.Total_Floor=int.Parse(dt.Rows[n]["Total_Floor"].ToString());
					}
					if(dt.Rows[n]["AppCityId"].ToString()!="")
					{
						model.AppCityId=new Guid(dt.Rows[n]["AppCityId"].ToString());
					}
					model.AppCityName=dt.Rows[n]["AppCityName"].ToString();
					if(dt.Rows[n]["AppCompanyId"].ToString()!="")
					{
						model.AppCompanyId=new Guid(dt.Rows[n]["AppCompanyId"].ToString());
					}
					model.AppCompanyName=dt.Rows[n]["AppCompanyName"].ToString();
					if(dt.Rows[n]["AppDepartId"].ToString()!="")
					{
						model.AppDepartId=new Guid(dt.Rows[n]["AppDepartId"].ToString());
					}
					model.AppDepartName=dt.Rows[n]["AppDepartName"].ToString();
					if(dt.Rows[n]["AppEmployeeId"].ToString()!="")
					{
						model.AppEmployeeId=new Guid(dt.Rows[n]["AppEmployeeId"].ToString());
					}
					model.AppEmployeeName=dt.Rows[n]["AppEmployeeName"].ToString();
					model.AppEmployeeCode=dt.Rows[n]["AppEmployeeCode"].ToString();
                    if (dt.Rows[n]["PreliminaryCityId"].ToString() != "")
                    {
                        model.PreliminaryCityId = new Guid(dt.Rows[n]["PreliminaryCityId"].ToString());
                    }
                    model.PreliminaryCityName = dt.Rows[n]["PreliminaryCityName"].ToString();
					if(dt.Rows[n]["PreliminaryCompanyId"].ToString()!="")
					{
						model.PreliminaryCompanyId=new Guid(dt.Rows[n]["PreliminaryCompanyId"].ToString());
					}
					model.PreliminaryCompanyName=dt.Rows[n]["PreliminaryCompanyName"].ToString();
					if(dt.Rows[n]["PreliminaryDepartId"].ToString()!="")
					{
						model.PreliminaryDepartId=new Guid(dt.Rows[n]["PreliminaryDepartId"].ToString());
					}
					model.PreliminaryDepartName=dt.Rows[n]["PreliminaryDepartName"].ToString();
					if(dt.Rows[n]["LoanTotalMoney"].ToString()!="")
					{
						model.LoanTotalMoney=decimal.Parse(dt.Rows[n]["LoanTotalMoney"].ToString());
					}
					model.LoanInfo=dt.Rows[n]["LoanInfo"].ToString();
					if(dt.Rows[n]["OperateQuanZ"].ToString()!="")
					{
						if((dt.Rows[n]["OperateQuanZ"].ToString()=="1")||(dt.Rows[n]["OperateQuanZ"].ToString().ToLower()=="true"))
						{
						model.OperateQuanZ=true;
						}
						else
						{
							model.OperateQuanZ=false;
						}
					}
                    if (dt.Rows[n]["EmpCityId"].ToString() != "")
                    {
                        model.EmpCityId = new Guid(dt.Rows[n]["EmpCityId"].ToString());
                    }
                    model.EmpCityName = dt.Rows[n]["EmpCityName"].ToString();
					if(dt.Rows[n]["EmpCompanyId"].ToString()!="")
					{
						model.EmpCompanyId=new Guid(dt.Rows[n]["EmpCompanyId"].ToString());
					}
					model.EmpCompanyName=dt.Rows[n]["EmpCompanyName"].ToString();
					if(dt.Rows[n]["EmpDepartId"].ToString()!="")
					{
						model.EmpDepartId=new Guid(dt.Rows[n]["EmpDepartId"].ToString());
					}
					model.EmpDepartName=dt.Rows[n]["EmpDepartName"].ToString();
					if(dt.Rows[n]["EmployeeId"].ToString()!="")
					{
						model.EmployeeId=new Guid(dt.Rows[n]["EmployeeId"].ToString());
					}
					model.EmployeeName=dt.Rows[n]["EmployeeName"].ToString();
                    model.EmployeeCode = dt.Rows[n]["EmployeeCode"].ToString();
					model.EmpPhone=dt.Rows[n]["EmpPhone"].ToString();
                    if (dt.Rows[n]["Receipt"].ToString() != "")
                    {
                        if ((dt.Rows[n]["Receipt"].ToString() == "1") || (dt.Rows[n]["Receipt"].ToString().ToLower() == "true"))
                        {
                            model.Receipt = true;
                        }
                        else
                        {
                            model.Receipt = false;
                        }
                    }
                    if (dt.Rows[n]["Mortgage"].ToString() != "")
                    {
                        if ((dt.Rows[n]["Mortgage"].ToString() == "1") || (dt.Rows[n]["Mortgage"].ToString().ToLower() == "true"))
                        {
                            model.Mortgage = true;
                        }
                        else
                        {
                            model.Mortgage = false;
                        }
                    }
                    model.SaleName = dt.Rows[n]["SaleName"].ToString();
                    model.SaleIDCard = dt.Rows[n]["SaleIDCard"].ToString();
                    model.SaleSpouseeName = dt.Rows[n]["SaleSpouseeName"].ToString();
                    model.SaleSpouseeIDCard = dt.Rows[n]["SaleSpouseeIDCard"].ToString();
                    model.OldLoanBank = dt.Rows[n]["OldLoanBank"].ToString();
                    if (dt.Rows[n]["OldLoanMoney"].ToString() != "")
                    {
                        model.OldLoanMoney = decimal.Parse(dt.Rows[n]["OldLoanMoney"].ToString());
                    }
                    if (dt.Rows[n]["OldRestMoney"].ToString() != "")
                    {
                        model.OldRestMoney = decimal.Parse(dt.Rows[n]["OldRestMoney"].ToString());
                    }
                    if (dt.Rows[n]["IsOneYear"].ToString() != "")
                    {
                        if ((dt.Rows[n]["IsOneYear"].ToString() == "1") || (dt.Rows[n]["IsOneYear"].ToString().ToLower() == "true"))
                        {
                            model.IsOneYear = true;
                        }
                        else
                        {
                            model.IsOneYear = false;
                        }
                    }
                    model.Remark = dt.Rows[n]["Remark"].ToString();
					if(dt.Rows[n]["PreliminaryApplyDate"].ToString()!="")
					{
						model.PreliminaryApplyDate=DateTime.Parse(dt.Rows[n]["PreliminaryApplyDate"].ToString());
					}
					if(dt.Rows[n]["EstimatePrice"].ToString()!="")
					{
						model.EstimatePrice=decimal.Parse(dt.Rows[n]["EstimatePrice"].ToString());
					}
					model.ProposeBank=dt.Rows[n]["ProposeBank"].ToString();
					if(dt.Rows[n]["GJJmoney"].ToString()!="")
					{
						model.GJJmoney=decimal.Parse(dt.Rows[n]["GJJmoney"].ToString());
					}
					model.GJJyear=dt.Rows[n]["GJJyear"].ToString();
					if(dt.Rows[n]["SYMoney"].ToString()!="")
					{
						model.SYMoney=decimal.Parse(dt.Rows[n]["SYMoney"].ToString());
					}
					model.SYYear=dt.Rows[n]["SYYear"].ToString();
					model.ReplyAdivce=dt.Rows[n]["ReplyAdivce"].ToString();
					if(dt.Rows[n]["ReplyCompanyId"].ToString()!="")
					{
						model.ReplyCompanyId=new Guid(dt.Rows[n]["ReplyCompanyId"].ToString());
					}
					model.ReplyCompanyName=dt.Rows[n]["ReplyCompanyName"].ToString();
					if(dt.Rows[n]["ReplyDepartId"].ToString()!="")
					{
						model.ReplyDepartId=new Guid(dt.Rows[n]["ReplyDepartId"].ToString());
					}
					model.ReplyDepartName=dt.Rows[n]["ReplyDepartName"].ToString();
					if(dt.Rows[n]["ReplyUserId"].ToString()!="")
					{
						model.ReplyUserId=new Guid(dt.Rows[n]["ReplyUserId"].ToString());
					}
					model.ReplyUserName=dt.Rows[n]["ReplyUserName"].ToString();
					if(dt.Rows[n]["ReplyPreliminaryDate"].ToString()!="")
					{
						model.ReplyPreliminaryDate=DateTime.Parse(dt.Rows[n]["ReplyPreliminaryDate"].ToString());
					}
					if(dt.Rows[n]["LoanApplyDate"].ToString()!="")
					{
						model.LoanApplyDate=DateTime.Parse(dt.Rows[n]["LoanApplyDate"].ToString());
					}
                    model.OrderPlace = dt.Rows[n]["OrderPlace"].ToString();
					model.OrderBank=dt.Rows[n]["OrderBank"].ToString();
					model.OrderBeiZhu=dt.Rows[n]["OrderBeiZhu"].ToString();
					if(dt.Rows[n]["LoanReplyDate"].ToString()!="")
					{
						model.LoanReplyDate=DateTime.Parse(dt.Rows[n]["LoanReplyDate"].ToString());
					}
					if(dt.Rows[n]["PreliminaryStatus"].ToString()!="")
					{
						model.PreliminaryStatus=int.Parse(dt.Rows[n]["PreliminaryStatus"].ToString());
					}
					if(dt.Rows[n]["LastDate"].ToString()!="")
					{
						model.LastDate=DateTime.Parse(dt.Rows[n]["LastDate"].ToString());
					}
					model.delName=dt.Rows[n]["delName"].ToString();
					if(dt.Rows[n]["delUserId"].ToString()!="")
					{
						model.delUserId=new Guid(dt.Rows[n]["delUserId"].ToString());
					}
					if(dt.Rows[n]["delTime"].ToString()!="")
					{
						model.delTime=DateTime.Parse(dt.Rows[n]["delTime"].ToString());
					}
					if(dt.Rows[n]["ContractId"].ToString()!="")
					{
						model.ContractId=new Guid(dt.Rows[n]["ContractId"].ToString());
					}
					model.ContractCode=dt.Rows[n]["ContractCode"].ToString();
					if(dt.Rows[n]["ContractDate"].ToString()!="")
					{
						model.ContractDate=DateTime.Parse(dt.Rows[n]["ContractDate"].ToString());
					}
					if(dt.Rows[n]["OperateCompanyId"].ToString()!="")
					{
						model.OperateCompanyId=new Guid(dt.Rows[n]["OperateCompanyId"].ToString());
					}
					model.OperateCompanyName=dt.Rows[n]["OperateCompanyName"].ToString();
					if(dt.Rows[n]["OperateDepartId"].ToString()!="")
					{
						model.OperateDepartId=new Guid(dt.Rows[n]["OperateDepartId"].ToString());
					}
					model.OperateDepartName=dt.Rows[n]["OperateDepartName"].ToString();
					if(dt.Rows[n]["OperateUserId"].ToString()!="")
					{
						model.OperateUserId=new Guid(dt.Rows[n]["OperateUserId"].ToString());
					}
					model.OperateUserName=dt.Rows[n]["OperateUserName"].ToString();
                    model.SpecialExp = dt.Rows[n]["SpecialExp"].ToString();
                    if (dt.Rows[n]["PreliminaryDate"].ToString() != "")
                    {
                        model.PreliminaryDate = DateTime.Parse(dt.Rows[n]["PreliminaryDate"].ToString());
                    }
                    if (dt.Rows[n]["LoanDate"].ToString() != "")
                    {
                        model.LoanDate = DateTime.Parse(dt.Rows[n]["LoanDate"].ToString());
                    }
                    modelList.Add(model);
                }
            }
            return modelList;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetAllList()
        {
            return GetList("");
        }

        //*******************************************
        //功能概要       ：根据参数列表获得用户信息一览
        //参数説明       ：hsParams 参数列表
        //返回值説明     ：无
        //功能詳細       ：根据参数列表获得用户信息一览
        //作成者         ：zou.xiaochun
        //作成日         ：2010-07-14
        //*******************************************
        public DataSet GetModelListByCoditions(string strWhere, int strPageIndex, int strPageSize)
        {
            //排序
            string strOrderField = " LastDate desc";
            //欲选择字段列表 
            string FieldList = " RecordId,PreliminaryStatus,LoanApplyDate,OrderPlace,OrderBank,Address,GJJmoney,GJJyear,SYMoney,SYYear,SalePrice_T,EstimatePrice,LoanTotalMoney,LoanInfo,EmpDepartName,EmployeeName,EmployeeCode,EmpPhone,OperateQuanZ,CompleteYear,Total_Floor,BuildingArea,BuildingType,CustomerName,IDNum,GJJNum,SpousesName,SpousesIDNum,SpousesGJJNum,SpecialExp,PreliminaryDate,LoanDate,AppDepartName,AppEmployeeName,AppEmployeeCode,PreliminaryApplyDate ";
            //子查询欲选择字段列表
            StringBuilder sb = new StringBuilder();
            sb.Append(" a.RecordId ,a.PreliminaryStatus,a.LoanApplyDate,a.OrderPlace,a.OrderBank,a.Address,a.GJJmoney,a.GJJyear,a.SYMoney,a.SYYear,a.SalePrice_T,a.EstimatePrice,a.LoanTotalMoney,a.LoanInfo,a.EmpDepartName,a.EmployeeName,a.EmployeeCode,a.EmpPhone,a.OperateQuanZ,a.CompleteYear,a.Total_Floor,a.BuildingArea,a.BuildingType,b.CustomerName,b.IDNum,b.GJJNum,b.SpousesName,b.SpousesIDNum,b.SpousesGJJNum,a.SpecialExp,a.PreliminaryDate,a.LoanDate,a.AppDepartName,a.AppEmployeeName,a.AppEmployeeCode,a.PreliminaryApplyDate ");

            //所要查询的表名
            string TableName = " t_Preliminary_BaseInfo a left join (select c.* from t_Preliminary_SubInfo1 c where c.IDNum in ( select max(IDNum) from t_Preliminary_SubInfo1 d where c.MainId = d.MainId And d.BOwner = '1' )) b on a.RecordId = b.MainId ";
            return Chenxin.Lib.Business.DAL.t_Preliminary_BaseInfo.GetList(strPageIndex, strPageSize, strOrderField, TableName, strWhere, FieldList, sb.ToString(), true);
        }
        #endregion  成员方法
    }
}